<?php
/**
 * vim: et ts=4 sts=4 sw=4 fdm=marker
 *
 * 控制器 
 *
 * @author	wang Penghai <china_php@hotmail.com>
 * @copyright 2008 5e6d Corporation.
 * @version   $Id: config.php 29 2009-03-10 14:30:18Z wph $
 */
//begin 宏定义
if (!defined('VIEW_CACHE_ON'))
{
	define('VIEW_CACHE_ON', FALSE);
}
if (!defined('VIEW_CACHE_TIME'))
{
	define('VIEW_CACHE_TIME', 60);
}
//end 宏定义

class pf_controller {

	protected $_tpl_path = '';
	protected $_is_cache = VIEW_CACHE_ON;//是否缓存
	protected $_cache_time = VIEW_CACHE_TIME;//缓存时间，单位是秒
	var $_tpl_vars = array();
	/**
	 * 构造函数
	 *
	 * @return void
	 */
	public function __construct()
	{
	}

	/**
	 * 模板引擎
	 * 
	 * @param $tpl_name 模板名称
	 * @param $tpl_path 模板路径
	 *  
	 */
	public function view($tpl_name)
	{
		$tpl_file = APP_PATH . str_replace('_', DIRECTORY_SEPARATOR, $tpl_name) . '.tpl.php';
		//模板文件是否存在
		if (!is_file($tpl_file))
		{
			die($tpl_file . ' tpl is not exist!');
		}

		$path_parts = pathinfo($tpl_file);
		$this->_tpl_path = $path_parts["dirname"];
		if (!empty($this->_tpl_vars))
		{
			@extract($this->_tpl_vars);
		}
		include_once($tpl_file);
	}

	/**
	 * assigns values to template variables
	 *
	 * @param array|string $tpl_var the template variable name(s)
	 * @param mixed $value the value to assign
	 */
	public function assign($tpl_var, $value = null)
	{
		if (is_array($tpl_var)){
			foreach ($tpl_var as $key => $val) {
				if ($key != '') {
					$this->_tpl_vars[$key] = $val;
				}
			}
		} else {
			if ($tpl_var != '')
			$this->_tpl_vars[$tpl_var] = $value;
		}
	}

	public function set_css($css, $sub_path='')
	{
		if ($sub_path !== '')
		{
			return BASE_URL .'resouce/images/' . $sub_path .'/' . $css;
		}

		return BASE_URL . 'resouce/images/' . $css;
	}

	public function set_img($img, $sub_path='')
	{
		return $this->set_css($img, $sub_path);
	}

	public function set_js($js, $sub_path='')
	{
		if ($sub_path !== '')
		{
			return BASE_URL . '/resouce/js/' . $sub_path . '/' . $js;
		}

		return BASE_URL .'resouce/js/' . $js;
	}

	/**
	 * url 规则
	 */
	public function set_url($model='',$controller='', $action='', $params=array())
	{
		if (!defined('APP_NAME'))
		{
			$app_name = 'index';
		} else {
			$app_name = APP_NAME;
		}
		$param = '';
		if (!empty($params) && is_array($params))
		{
			foreach ($params as $key => $val)
			{
				$param .= '&' . $key . '=' . $val;
			}
		}
		$url = $app_name . APP_NAME_EXT;
		if ($model !== '')
		{
			$url .= '?' . R_MODEL_KEY . '=' . $model ;
		}

		if ($controller !== '')
		{
			$url .= '&' . R_CONTROLLER_KEY . '=' . $controller ;
		}

		if ($action !== '')
		{
			$url .= '&' . R_ACTION_KEY . '=' . $action;
		}
		$url .= $param;

		/* author:dingxiangning 2011.11.18 */
		if(isset($_SESSION['pc_hash'])){
			$url .="&pc_hash=".$_SESSION['pc_hash'];
		}
		
		return BASE_URL . $url;
	}
}
